package cst.a931xmc.yuejian20.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cst.a931xmc.yuejian20.Activity.ClubActivity;
import cst.a931xmc.yuejian20.Bean.ClubBean;
import cst.a931xmc.yuejian20.Model.ApiModel;
import cst.a931xmc.yuejian20.R;

public class ClubAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private LayoutInflater inflater;
    private List<ClubBean> list;
    private Context context;

    public ClubAdapter(Context context){
        inflater = LayoutInflater.from(context);
        this.context = context;
    }

    public void setList(List<ClubBean> l) {
        list = l;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = inflater.from(parent.getContext()).inflate(R.layout.item_club, parent, false);
        RecyclerView.ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ClubBean bean = list.get(position);
        if (bean == null)
            return;
        final ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.club_name.setText(bean.getClub_name());
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = viewHolder.getLayoutPosition();
                Intent intent;
                intent = new Intent(context, ClubActivity.class);
                intent.putExtra("club_id", list.get(position).getClub_id());
                context.startActivity(intent);
            }
        });

        ApiModel apiModel = new ApiModel();
        Picasso.with(context)
                .load(apiModel.getBaseUrl() + "static/images/" + bean.getClub_pic())
                .placeholder(R.drawable.nopic)
                .into(viewHolder.club_pic);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if (list == null) return 0;
        else return list.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView club_pic;
        private TextView club_name;

        public ViewHolder(View itemView) {
            super(itemView);
            club_pic = itemView.findViewById(R.id.club_pic);
            club_name = itemView.findViewById(R.id.club_name);
        }
    }

}
